package com.banwoqiche.service.impl;
import com.banwoqiche.dto.MyBackCode;
import com.banwoqiche.exceptions.AddErrorException;
import com.banwoqiche.exceptions.MyBaseException;
import com.banwoqiche.mapper.BindMapper;
import com.banwoqiche.mapper.SysMenusMapper;
import com.banwoqiche.mapper.SysRoleMapper;
import com.banwoqiche.pojo.SysMenu;
import com.banwoqiche.pojo.SysRole;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @time: 2022-03-31 23:55
 */
@Service
@Transactional
public class BindHandleServiceImpl implements BindHandleService {
    private SysRoleMapper rankMapper;
    private SysMenusMapper menusMapper;
    private BindMapper bindMapper;

    @Autowired
    public void setRankMapper(SysRoleMapper sysroleMapper) {
        this.rankMapper = rankMapper;
    }

    @Autowired
    public void setMenusMapper(SysMenusMapper menusMapper) {
        this.menusMapper = menusMapper;
    }

    @Autowired
    public void setBindMapper(BindMapper bindMapper) {
        this.bindMapper = bindMapper;
    }

    @Override
    public List<SysRole> getAllRank() {
        return rankMapper.getAllRank();
    }

    @Override
    public List<SysMenu> getAllMenus() {
        return menusMapper.getAllMenus();
    }

    @Override
    public int addRank_Menus(Long rankId, Long menuId) {
        if (rankId == null || menuId == null) {
            throw new AddErrorException(">addRank_Menus->dataNull", MyErrCode.DATA_NULL);
        }
        int backId = bindMapper.checkRank_Menus(rankId, menuId);
        Assert.isNull(backId, () -> {
            throw new AddErrorException(">addRank_Menus->BindAlreadyExist", MyErrCode.INPUT_DATE_ISEXIST);
        });
        return bindMapper.bindRankAndMenus(rankId, menuId);
    }
    @Override
    public void removeRank_Menu(Long rankId, Long menusId) {
        if (rankId == 1) {
            throw new MyBaseException(">removeRank_Menu->notAllowToChangeSuperAdmin", MyErrCode.FAIL);
        }
        bindMapper.removeRank_Menus(rankId, menusId);
    }
    @Override
    public void removeRank_AllMenu(Long rankId) {
        if (rankId == 1) {
            throw new MyBaseException(">removeRank_AllMenu->notAllowToChangeSuperAdmin", MyErrCode.FAIL);
        }
        bindMapper.removeRankAllMenus(rankId);
    }
    @Override
    public int addUser_Rank(Long userId, Long rankId) {
        if (userId == null || rankId == null) {
            throw new AddErrorException(">addUser_Rank->dataNull", MyErrCode.DATA_NULL);
        }
        int backId = bindMapper.checkUser_Rank(userId, rankId);
        Assert.isNull(backId, () -> {
            throw new AddErrorException(">addUser_Rank->BindAlreadyExist", MyErrCode.INPUT_DATE_ISEXIST);
        });
        return bindMapper.bindUserAndRank(userId, rankId);
    }
    @Override
    public void updateUser_Rank(Long userId, Long rankId) {
        if (userId == null || rankId == null) {
            throw new AddErrorException(">updateUser_Rank->dataNull", MyErrCode.DATA_NULL);
        }
        int i = bindMapper.changeUser_Rank(userId, rankId);
    }
}
